package question;

import java.util.Arrays;

import track.TriathlonTrack;
import athlete.Athlete;

public class Question {

	public static void main(String[] args) throws InterruptedException {
		int numOfParticipants = 10;
		Athlete[] athletes = AthleteCreator.createAthletes(numOfParticipants);
		TriathlonTrack track = new TriathlonTrack();
		
		AthleteRunnable[] athleteRunnables = AthleteCreator.createAthleteRunnables(numOfParticipants, athletes, track);
		
		Thread[] athleteThreads = new Thread[numOfParticipants];
		
		for(int i=0; i<numOfParticipants; i++){
			athleteThreads[i] = new Thread(athleteRunnables[i]);
		}

		for(int i=0; i<numOfParticipants; i++){
			athleteThreads[i].start();
			athleteThreads[i].join();
		}
		printOutputs(athleteRunnables);
	}
	
	public static void printOutputs(AthleteRunnable[] athleteRunnables){
		Arrays.sort(athleteRunnables);
		System.out.printf("%s%20s%10s%10s%10s%10s\n", "#", "Name", "RunTime", "CycleTime", "SwimTime", "TotalTime");
		int i=0;
		for(AthleteRunnable ath: athleteRunnables){
			System.out.printf("%d%20s%10s%10s%10s%10s\n", i++,
					ath.getAthleteName(), 
					ath.getRunningTime(), 
					ath.getCyclingTime(), 
					ath.getSwimmingTime(), 
					ath.getTotalTime());
		}
	}

}
